﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WcfiTrackingMvc4
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class iTrackingServices : System.Web.Services.WebService
    {

        private iTrackingMvc4Entities _db = new iTrackingMvc4Entities();

        [WebMethod]
        public Article GetArticle(Guid articleId)
        {            
            try
            {
                return _db.Articles.Find(articleId);                                
            }
            catch
            {
                return null;
            }
        }

        [WebMethod]
        public Article CreateArticle(string title, string summary, string content, string author)
        {
            Article _article = new Article();
            _article.Id = Guid.NewGuid();
            _article.Title = title;
            _article.Summary = summary;
            _article.Content = content;

            _article.CreateBy = author;
            _article.CreateDate = DateTime.Now;
            _article.LastUpdate = DateTime.Now;
            _article.UpdateBy = author;

            _article.IsApproved = true;
            _article.ApprovedBy = author;
            _article.ApprovedDate = DateTime.Now;

            _article.CountViews = 0;
            //_article.CategoryId = _db.ArticleCategories.FirstOrDefault().Id;

            try
            {
                _db.Articles.Add(_article);
                _db.SaveChanges();
                return _article;
            }
            catch
            {
                return null;
            }
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}
